package com.talent.jiwen.ui.widgets;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.talent.jiwen.R;
import com.talent.jiwen.model.GradeInfo;
import com.talent.jiwen.training.data.IdNameBean;
import com.talent.jiwen.ui.widgets.flowlayout.FlowLayout;
import com.talent.jiwen.ui.widgets.flowlayout.TagAdapter;
import com.talent.jiwen.ui.widgets.flowlayout.TagFlowLayout;
import com.talent.jiwen.util.Constant;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class ChooseSubjectDialog extends Dialog {

    TagFlowLayout subjectLayout;

    List<IdNameBean> subjectList = new ArrayList<>();

    private Activity mContext;
    private OnTagClickListener listener;

    public ChooseSubjectDialog(Activity context) {
        super(context, R.style.myDialogTheme);
        this.mContext = context;
    }

    public ChooseSubjectDialog(Activity context,OnTagClickListener listener) {
        super(context, R.style.stardialog);
        this.mContext = context;
        this.listener=listener;
    }

    protected ChooseSubjectDialog(Activity context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        this.mContext = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initData();
        View view = LayoutInflater.from(mContext).inflate(R.layout.dialog_choose_subject, null);
        setContentView(view);

        //设置dialog大小
        Window dialogWindow = getWindow();
        dialogWindow.setWindowAnimations(R.style.dialogWindowAnim); //设置窗口弹出动画
        WindowManager manager = ((Activity) mContext).getWindowManager();
        WindowManager.LayoutParams params = dialogWindow.getAttributes(); // 获取对话框当前的参数值
        dialogWindow.setGravity(Gravity.CENTER);
        Display d = manager.getDefaultDisplay(); // 获取屏幕宽、高度
        params.width = (int) (d.getWidth() * 0.9);
        params.height = LinearLayout.LayoutParams.WRAP_CONTENT;
        dialogWindow.setAttributes(params);
        setCanceledOnTouchOutside(true);
        initView();
    }

    private void initData(){
        subjectList.clear();
        for (Map.Entry<Integer, String> entry : Constant.subjectMap.entrySet()) {
            IdNameBean bean = new IdNameBean(entry.getKey() ,entry.getValue());
            subjectList.add(bean);
        }
    }

    private void initView() {

        subjectLayout = (TagFlowLayout) findViewById(R.id.subjectLayout);
        TagAdapter<IdNameBean> subJectAdapter = new TagAdapter<IdNameBean>(subjectList) {
            @Override
            public View getView(FlowLayout parent, int position, final IdNameBean idNameBean) {
                TextView tv = (TextView) mContext.getLayoutInflater().inflate(R.layout.tv_tag,
                        subjectLayout, false);
                tv.setText(idNameBean.getName());

                tv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        listener.onClick(ChooseSubjectDialog.this,idNameBean);
                        ChooseSubjectDialog.this.dismiss();
                    }
                });

                ChooseSubjectDialog.this.dismiss();

                return tv;
            }
        };

        subjectLayout.setAdapter(subJectAdapter);

    }

    public interface OnTagClickListener{
        void onClick(Dialog dialog, IdNameBean idNameBean);
    }

}
